package com.prp.db.daoimpl.mysql;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Projections;

import com.prp.db.idao.IRecommendationEntityDAO;
import com.prp.db.po.RecommendationEntity;

/**
 * A data access object (DAO) providing persistence and search support for
 * RecommendationEntity entities. Transaction control of the save(), update()
 * and delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.prp.db.daoimpl.mysql.RecommendationEntity
 * @author MyEclipse Persistence Tools
 */

public class RecommendationEntityDAO extends BaseHibernateDAO implements IRecommendationEntityDAO{
	private static final Log log = LogFactory
			.getLog(RecommendationEntityDAO.class);
	// property constants
	public static final String ENTITY_NAME = "entityName";
	public static final String CLICK_FREQ = "clickFreq";
	public static final String WAITING_TIME = "waitingTime";
	public static final String REPLY_NUM = "replyNum";
	public static final String CHILD_CATEGORY_ID = "childCategoryId";

	public void save(RecommendationEntity transientInstance) {
		log.debug("saving RecommendationEntity instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(RecommendationEntity persistentInstance) {
		log.debug("deleting RecommendationEntity instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public RecommendationEntity findById(java.lang.Integer id) {
		log.debug("getting RecommendationEntity instance with id: " + id);
		try {
			RecommendationEntity instance = (RecommendationEntity) getSession()
					.get(RecommendationEntity.class, id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(RecommendationEntity instance) {
		log.debug("finding RecommendationEntity instance by example");
		try {
			List results = getSession().createCriteria(
					"com.prp.db.daoimpl.mysql.RecommendationEntity").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByChildCategoryId(int categoryId){
		return findByProperty(CHILD_CATEGORY_ID, categoryId);
	}
	
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding RecommendationEntity instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from RecommendationEntity as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByEntityName(Object entityName) {
		return findByProperty(ENTITY_NAME, entityName);
	}

	public List findByClickFreq(Object clickFreq) {
		return findByProperty(CLICK_FREQ, clickFreq);
	}

	public List findByWaitingTime(Object waitingTime) {
		return findByProperty(WAITING_TIME, waitingTime);
	}

	public List findByReplyNum(Object replyNum) {
		return findByProperty(REPLY_NUM, replyNum);
	}

	public List findAll() {
		log.debug("finding all RecommendationEntity instances");
		try {
			String queryString = "from RecommendationEntity";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public RecommendationEntity merge(RecommendationEntity detachedInstance) {
		log.debug("merging RecommendationEntity instance");
		try {
			RecommendationEntity result = (RecommendationEntity) getSession()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(RecommendationEntity instance) {
		log.debug("attaching dirty RecommendationEntity instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(RecommendationEntity instance) {
		log.debug("attaching clean RecommendationEntity instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	
	@Override
	public List<RecommendationEntity> searchByPattern(String query) {
		log.debug("find string by name pattern");
		try {
			String searchQuery = "from RecommendationEntity as model where model.entityName like ?";
			Query searchQueryObject = getSession().createQuery(searchQuery);
			searchQueryObject.setParameter(0, query);
			List<RecommendationEntity> list = searchQueryObject.list();
			return list;
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	@Override
	public List findRecentEntity() {
		Criteria criteria = getSession().createCriteria(RecommendationEntity.class);
		Integer id = (Integer)criteria.setProjection(Projections.max("entityId")).list().get(0);
		String query = "from RecommendationEntity as model where model.entityId > ?";
		Query queryObject = getSession().createQuery(query);
		queryObject.setParameter(0, id-10);
		return queryObject.list();
	}
	
}